﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using TianYu.Core.Pdf.ContentElement.Text;

namespace TianYu.Core.Pdf.ContentElement.Circle
{
    public class CircleElement : AbstractElement
    {
        /// <summary> 边框 </summary>
        public float Border { get; set; }

        /// <summary> 自定义内容 </summary>
        public ContentModel Content { get; set; }

        public override void Draw(PdfTemplateContent pdfContent)
        {
            var r = Height / 2; //圆半经
            //画圆 
            pdfContent.PdfWriter.DirectContent.SetLineWidth(Border / CommonHelper.LineWidthReduce); //细1.5倍是最接近后台设置的粗细
            pdfContent.PdfWriter.DirectContent.SetColorStroke(BaseColor.BLACK);
            pdfContent.PdfWriter.DirectContent.Circle(X + r, Y + r, r);
            pdfContent.PdfWriter.DirectContent.SetLineDash(1);
            pdfContent.PdfWriter.DirectContent.Stroke();
            pdfContent.PdfWriter.DirectContent.SetLineDash(0);//重置回去，否则后续画线都已最后画线虚实为准

            //画圆中文字
            if (!string.IsNullOrWhiteSpace(Content.Text))
            {
                var pdfContentByte = pdfContent.PdfWriter.DirectContent;

                var columnText = new ColumnText(pdfContentByte);

                var rectBorder = new Rectangle(X, Y, X + Height, Y + r - Content.Size / 2 + 2);

                pdfContentByte.Rectangle(rectBorder);
                columnText.SetSimpleColumn(rectBorder);

                var paragraph = new Paragraph(Content.RowSpace * CommonHelper.BaseLeading);

                var fontStyle = CommonHelper.GetFontStyle(Content.Style);
                Font family = new Font(CommonHelper.GetFontFamilyPath(Content.Family), Content.Size, fontStyle, BaseColor.BLACK);

                paragraph.SpacingBefore = 1;
                paragraph.Font = family;
                paragraph.Alignment = CommonHelper.GetAlignment("center");
                paragraph.Add(Content.Text);

                columnText.AddElement(paragraph);

                columnText.Go();
            }
        }
    }
}
